import { networkInterfaces } from 'os';

let EARTH_RADIUS = 6378137.0;    //单位m
let PI = Math.PI;

function getRad(d){
    return d*PI/180.0;
}
export function rand(min: number, max: number) {
    return Math.floor(Math.random() * (max-min)) + min
}
export function uuid(len: number, radix?: number, onlyLowerCase?: boolean) {
    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    if (onlyLowerCase) {
        chars = 'abcdefghijklmnopqrstuvwxyz'.split('');
    }
    const uuid = [];
    let i;
    radix = radix || chars.length;
    if (len) {
// Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
// rfc4122, version 4 form
        let r;
// rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
// Fill in random data.  At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join('');
}
export function zeroNum(num){
    return (num + '').length < 2 ? ('0' + num) : num;
}
export function formatTime(time) {
    try {
        const date = new Date(time);
        return date.getFullYear() + '-' + zeroNum(Number(date.getMonth() + 1)) +  '-' + zeroNum(date.getDate()) + ' ' + zeroNum(date.getHours()) + ':' + zeroNum(date.getMinutes()) + ':' + zeroNum(date.getSeconds());
    } catch(e) {
        return ''
    }
}
export function getGreatCircleDistance(lat1,lng1,lat2,lng2){
    let radLat1 = getRad(lat1);
    let radLat2 = getRad(lat2);

    let a = radLat1 - radLat2;
    let b = getRad(lng1) - getRad(lng2);

    let s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s*EARTH_RADIUS;
    s = Math.round(s*10000)/10000.0;

    return s;
}

/** 计算两经纬度之间的距离，单位是m
 * approx distance between two points on earth ellipsoid
 */
export function getFlatternDistance (lat1, lng1, lat2, lng2) {
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s * 1000
    /*
    const PI = Math.PI
    const EARTH_RADIUS = 6378137.0
    function getRad (d) {
        return d * PI / 180.0
    }
    let f = getRad((lat1 + lat2) / 2)
    let g = getRad((lat1 - lat2) / 2)
    let l = getRad((lng1 - lng2) / 2)
    let sg = Math.sin(g)
    let sl = Math.sin(l)
    let sf = Math.sin(f)

    let s, c, w, r, d, h1, h2
    let a = EARTH_RADIUS
    let fl = 1 / 298.257

    sg = sg * sg
    sl = sl * sl
    sf = sf * sf

    s = sg * (1 - sl) + (1 - sf) * sl
    c = (1 - sg) * (1 - sl) + sf * sl

    w = Math.atan(Math.sqrt(s / c))
    r = Math.sqrt(s * c) / w
    d = 2 * w * a
    h1 = (3 * r - 1) / 2 / c
    h2 = (3 * r + 1) / 2 / s

    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg))
    */
}

// let result1 = getGreatCircleDistance(30.1234, 140.1234, 30.3456, 140.3456)
// let result2 = getFlatternDistance(30.1234, 140.1234, 30.3456, 140.3456)
// console.log(result1) // 32688.3298
// console.log(result2) // 32622.43244078783
export function isNumber(num) {
    return /^\d+$/.test(`${num}`)
}
export function getDaysBetween(startTime, endTime) {
    const sDate = new Date(startTime).getTime();
    const eDate = new Date(endTime).getTime();
    if (sDate > eDate) {
        return 0
    }
    if (sDate === eDate) {
        return 1
    }
    return (eDate - sDate) / (24 * 60 * 60 * 1000) + 1
}
export function getLogStr(str) {
    return `/********************************************************
    ${str}
    ********************************************************/\n`;
}
export function getIpMacAddress() {
    let ip = ''
    let mac = ''
    const nets = networkInterfaces();
    for (const name of Object.keys(nets)) {
        for (const net of nets[name]) {
            // 查找IPv4地址
            if (net.family === 'IPv4' && !net.internal && net.address !== '127.0.0.1' && net.mac !== '00:00:00:00:00:00') {
                ip = net.address;
                mac = net.mac;
            }
        }
    }
    return {ip,mac}
}

export function judgeCanUse(start: string, end: string, usedTime: string[]) {
    const today = new Date()
    const todayStr = today.getFullYear() + `-${today.getMonth()+1}` + `-${today.getDate()}`
    const intersection: string[] = [] // 缓存冲突的时间段
    usedTime.forEach((tm) => {
        const ta = tm.split('-')
        const ts = new Date(`${todayStr} ${ta[0]}:00`).getTime() // 时间段起点
        const te = new Date(`${todayStr} ${ta[1]}:00`).getTime() // 时间段终点
        const st = new Date(`${todayStr} ${start}:00`).getTime() // 开始时间
        const et = new Date(`${todayStr} ${end}:00`).getTime() // 结束时间
        /* 开始时间在时间段终点后 或 结束时间在时间段起点前 */
        if (st >= te || et <= ts) {

        } else {
            intersection.push(tm)
        }
    })
    console.log(start, end, intersection)
    return intersection
}
// const usedTime = ['08:05-08:35', '11:00-13:15', '13:45-16:05']
// judgeCanUse('07:10', '08:05', usedTime)
// judgeCanUse('07:50', '08:15', usedTime)
// judgeCanUse('09:10', '10:35', usedTime)
// judgeCanUse('16:05', '18:00', usedTime)
